package com.boot.controller;


import com.boot.entity.R;
import com.boot.entity.Stats;
import com.boot.entity.*;
import com.boot.service.CartService;
import com.boot.service.ItemService;
import com.boot.service.OrdersService;
import com.boot.service.ProductService;
import com.boot.utils.String2ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单表(Orders)表控制层
 *
 * @author makejava
 * @since 2023-07-20 18:48:25
 */
@RestController
@RequestMapping("/orders")
@CrossOrigin
public class OrdersController {
    /**
     * 服务对象
     */
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private CartService cartService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private ProductService productService;

    /**
     * 查询指定用户的所有订单
     * @param uid
     * @return
     */
    @RequestMapping("/list")
    public R queryAllOrdersByUid(@RequestParam("uid") Integer uid) {
        List<Orders> orders = ordersService.query().eq("u_id", uid).list();
        return R.success(orders);
    }

    @RequestMapping("/addOrders")
    @Transactional
    public R insert(
            @RequestParam("uId") Integer uid,
            @RequestParam("total")BigDecimal total,
            @RequestParam("cids") List<String> cids) {

        // 创建订单对象
        Orders orders = new Orders()
                .setUId(uid)
                .setTotal(total)
                .setId(UUID.randomUUID().toString())
                .setOTime(new Date());
        ordersService.save(orders);

        // 提交订单的购物车id
        List<Integer> cidList = String2ListUtils.s2List(cids.get(0));
        List<Cart> carts = cartService.listByIds(cidList);
        // 创建详情对象
        for (Cart cart : carts) {
            Product product = productService.queryOneOrAllProduct(cart.getPId()).get(0);
            Item item = new Item()
                    .setOId(orders.getId())
                    .setPId(cart.getPId())
                    .setCount(cart.getPCount())
                    .setSubTotal(product.getPrice().multiply(new BigDecimal(cart.getPCount())));
            System.out.println("newItem--->" + item);
            itemService.save(item);
        }
        // 删除已经提交订单的购物车
        cartService.removeByIds(cidList);
        return R.success("订单提交成功！");
    }

    /**
     * 统计消费最高的前十位用户
     * @return
     */
    @RequestMapping("/queryOrdersAndUserSales")
    public R queryOrdersAndUserSales(){

        List<Stats> list = ordersService.queryOrdersAndUserSales();
        List<String> s = new ArrayList<>();
        List<BigDecimal> b = new ArrayList<>();
        for (Stats stats : list) {
            s.add(stats.getStatsName());
            b.add(stats.getSum());
        }
        List l = new ArrayList();
        l.add(s);
        l.add(b);
        return R.success(l);
    }
}

